package lab_16.com.acme.bankapp.domain.bank;

import java.util.*;

public enum Gender {
	MALE("Mr.", "m"), FEMALE("Ms.", "f");

	private final String greet;
	private final String serial; // it's how the gender is called when written
									// to the file
	private static Map<String, Gender> genderBySerial = new HashMap<String, Gender>(); // keeps
																						// mapping
																						// from
																						// serializable
																						// format
																						// to
																						// Gender
																						// corresponding
																						// instances

	// Building map structure
	// This block is executed after enum instances is created.
	static {
		for (Gender gender : Gender.values()) {
			Gender.genderBySerial.put(gender.getSerialName(), gender);
		}
	}

	Gender(String greet, String serial) {
		this.greet = greet;
		this.serial = serial;
	}

	public String getGreeting() {
		return greet;
	}

	/**
	 * Get name for serialization
	 */
	public String getSerialName() {
		return serial;
	}

	/**
	 * @param s
	 *            Serial name (how it appears in the file)
	 * @return Corresponing Gender instance
	 */
	public static Gender getBySerialName(String s) {
		return Gender.genderBySerial.get(s);
	}
}